<template>
	<!-- 地址管理 -->
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)"  @longtap="lober(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.defaultAddress" class="tag">默认</text>
					<text class="address">{{item.consignee}} {{item.phone}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.province}}</text>
					<text class="name">{{item.city}}</text>
					<text class="name">{{item.county}}</text>
					<text class="name">{{item.address}}</text>
				</view>
			</view>
			<image src="../../static/gerzhox/xgai.png" class="icon-xiugai" @click.stop="addAddress('修改','edit', item)"></image>
		</view>
		<button class="add-btn" @click="addAddress('新增','add')">新增地址</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				source: 0,
				addressList: '',
				default: true,
			}
		},
		onLoad(option) {
			this.source = option.source;
			console.log(option.source);
		},
		onShow() {
			this.init()
			if (uni.getStorageSync('accessToken') == '') {
				uni.showModal({
					title: '登录提示',
					content: '您尚未登录，是否立即登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '../long/long'
							})
							return
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			}
		},
		methods: {
			checkAddress(item){
				if(this.source == 1){
					let pages = getCurrentPages(); //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1]; //当前页页面实例
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.addressData = item; //修改上一页data里面的searchVal参数值为1211
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: item
					});
				}
			},
			init(e) {
				console.log(uni.getStorageSync('accessToken'))
				uni.request({
					url: 'http://192.168.1.54:8081/m.api?_gp=address&_mt=getAllAddress',
					header: {
						'accesstoken': uni.getStorageSync('accessToken')
					},
					success: (res) => {
						// console.log(res,"13222222")
						this.addressList = res.data.data
					}
				})
			},
			lober(item) {
				// console.log(item.id)
				let _this = this
				uni.showModal({
					title: '删除提示',
					content: '您确定要删除该地址吗？',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: `http://192.168.1.54:8081/m.api?_gp=address&_mt=deleteAddress&addressId=${item.id}`,
								method: 'GET',
								header: {
									'accesstoken': uni.getStorageSync('accessToken')
								},
								success: function(res) {
									if (res.data.errmsg == '成功') {
										_this.init()
										uni.showToast({
											title: '删除成功',
										})
									}
								}
							})
						} 
					}
				})
			},
			addAddress(title,type, item) {
				console.log(title,type, item)
				uni.navigateTo({
					url: `/pages/address/modify?name=${title}&type=${type}&data=${encodeURIComponent(JSON.stringify(item))}`
				})
			},
		}
	}
</script>

<style scoped="scoped">
	page {padding-bottom: 120upx;}
	.b-b {border-bottom: 3rpx solid #F5F5F5;}
	.icon-xiugai{width: 45rpx; height: 45rpx;}
	.content {position: relative;}
	.list {display: flex;align-items: center;padding: 20upx 30upx;background: #fff;position: relative;}
	.wrapper {display: flex;flex-direction: column;flex: 1;}
	.address-box {display: flex;align-items: center;}
	.tag {font-size: 24upx;margin-right: 10upx;background: #fffafb;border: 1px solid #ffb4c7;border-radius: 4upx;padding: 4upx 10upx;line-height: 1;}
	.address {	font-size: 30upx;}
	.u-box {font-size: 28upx; margin-top: 16upx;}
	.name {margin-right: 30upx;}
	.icon-bianji {display: flex; align-items: center; height: 80upx; font-size: 40upx; padding-left: 30upx;}
	.add-btn {position: fixed; left: 30upx; right: 30upx; bottom: 16upx; z-index: 95;display: flex;align-items: center;
		justify-content: center; width: 690upx; height: 80upx; font-size: 32upx; color: #fff; background-color: #FA436A; border-radius: 10upx; box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);}
	.icon-liulanlishi {font-size: 50rpx;}
</style>